﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using RestUpMVC.Resource;
using RestUpMVC.Test.TestClasses;

namespace RestUpMVC.Test.Resource
{
    [TestClass]
    public class ResourceIdResolverTest
    {
        [TestMethod]
        [ExpectedException(typeof(ResourceIdNotFoundException))]
        public void ResolveMetaData_ShouldThrowAnExceptionIfTheResourceIdPropertyCannotBeDetermined()
        {
            var resourceIdResolver = new ResourceIdentityPropertyNameResolver();
            resourceIdResolver.ResolveMetaData(new { }.GetType(), new ResourceMetaData());
        }

        [TestMethod]
        [ExpectedException(typeof(ResourceIdNotFoundException))]
        public void ResolveMetaData_ShouldThrowAnExceptionIfTheTypeHasMoreThanOneIdProperty()
        {
            var idResolver = new ResourceIdentityPropertyNameResolver();
            idResolver.ResolveMetaData(new { Id = 1, id = 2 }.GetType(), new ResourceMetaData());
        }

        [TestMethod]
        public void ResolveMetaData_ShouldUseTheConfiguredResourceIdPropertyName()
        {
            RestUpMvcConfiguration.Instance.SetResourceIdPropertyName("foo");
            var metaData = new ResourceMetaData();
            var resourceIdResolver = new ResourceIdentityPropertyNameResolver();
            resourceIdResolver.ResolveMetaData(new { foo = "1" }.GetType(), metaData);

            Assert.AreEqual("foo", metaData.IdentityPropertyName);
        }
        
        [TestMethod]
        public void ResolveMetaData_ShouldFindThePropertyWithTheResourceIdAttribute()
        {
            var resourceIdResolver = new ResourceIdentityPropertyNameResolver();
            var metaData = new ResourceMetaData();
            resourceIdResolver.ResolveMetaData(typeof(TestResource), metaData);

            Assert.AreEqual("TheIdProperty", metaData.IdentityPropertyName);
        }
    }
}